﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Migrator.Entities;
using Migrator.Services.MobileIPNodeService;

namespace MigratorUI.UI
{
    public partial class LaptopPropertiesControl : UserControl
    {
        public LaptopPropertiesControl()
        {
            InitializeComponent();
        }

        public void LoadNode (Node node){
            if (node == null)
                return;
            
            this.node = node;
            
            if (node is MobileNode) {
                FillMobileNodeStatusTab();
            }
        }

        private void FillMobileNodeStatusTab(){
            tbRadius.Value = this.node.Radius;
            lbRadius.Text = this.node.Radius + "";
            this.lbX.Text = this.node.Location.X + "";
            this.lbY.Text = this.node.Location.Y + "";
            this.txtMACAddress.Text = this.node.MACAddress.ToString();
            this.txtIPAddress.Text = this.node.IPAddress.ToString();

            MobileIPNodeService service = (MobileIPNodeService)this.node.GetService(Migrator.Services.Services.MobileIPNodeService);
            if (service != null)
            {
                var reg = service.CurrentRegistration;
                if (reg != null)
                {
                    this.lblForeignAgent.Visible = true;
                    this.lblCareOfAddress.Visible = true;
                    this.lblExpirationTime.Visible = true;
                    this.lblCurrentRegistration.Visible = false;
                    this.lblFA.Visible = true;
                    this.lblCOA.Visible = true;
                    this.lblET.Visible = true;

                    this.lblForeignAgent.Text = reg.ForeignAgent.IPAddress.ToString();
                    this.lblCareOfAddress.Text = reg.CareOfAddress.ToString();
                    this.lblExpirationTime.Text = reg.ExpirationTime.ToString();
                }
                else
                {
                    this.lblCurrentRegistration.Visible = true;
                    this.lblForeignAgent.Visible = false;
                    this.lblCareOfAddress.Visible = false;
                    this.lblExpirationTime.Visible = false;
                    this.lblFA.Visible = false;
                    this.lblCOA.Visible = false;
                    this.lblET.Visible = false;
                }
            }
        }

        private void OnRadiusScroll(object sender, EventArgs e)
        {
            this.lbRadius.Text = this.tbRadius.Value + "";
            this.node.Radius = this.tbRadius.Value;
            
            //fires PropertyChangedEvent 
            if (PropertyChangedEvent != null) this.PropertyChangedEvent.Invoke(this.node);
        }

        private Node node;
        public delegate void PropertyChangedEventHandler(Node node);
        public event PropertyChangedEventHandler PropertyChangedEvent;
      
    }
}
